package top.chatgqt.SmartChargeStation.demo.netty.handlers;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;

/**
 * 自定义入站处理器
 */
@Slf4j
public class ImoocServerHandlerAdapter extends ChannelInboundHandlerAdapter {

    // 客户端连接触发
    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        log.info(">>>> 新的连接：{}", ctx.channel().id().asLongText());
        super.handlerAdded(ctx);
    }

    // 在接收到消息后触发
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        // 强制转换
        ByteBuf byteBuf = (ByteBuf) msg;
        String message = byteBuf.toString(StandardCharsets.UTF_8);

        log.info(">>>>> ChannelInboundHandlerAdapter收到的消息{}", message);

        // 将消息传递到下一个处理器
        // ctx.fireChannelRead(Unpooled.copiedBuffer(byteBuf));
    }
}
